/**
 * 简单表单验证类 cody by wangw
 * 使用方法
 * $(".btn-primary").click(function(){
    var mailcheck = checkform.reg('input01','input01hint','e#5.20#Email');
    var usercheck = checkform.reg('input02','input02hint','e#3.20#Url');
    return (mailcheck && usercheck);
    })
 * 
 */
var checkform = {
    Email: /^[a-zA-Z0-9._]{1,30}@[a-zA-Z0-9-]{1,20}\.[a-zA-Z0-9.-]{1,100}$/,
    Name: /^[a-zA-Z0-9._ ]{2,30}$/,
    Content: /^[a-zA-Z0-9._,.->:()$&%#@!~?'"]{8,400}$/,
    Subject: /^[a-zA-Z0-9._ ]{2,30}$/,
    Url: /^https?:\/\/[a-zA-Z0-9-]{1,20}\.[a-zA-Z0-9.-]{1,100}$/,
    Title: /^[a-zA-Z0-9.-]{8,100}$/,
    /**
     * [reg 通用表单验证方法]
     * @param  {[type]} id      [验证元素]
     * @param  {[type]} out_id  [输出提示信息的元素]
     * @param  {[type]} rule    [验证规则]
     *    e#4.10#email
     *    非空  长度 规则   用#格开
     * @param  {[type]} hintinfo[提示信息]
     * @return {[type]}         [返回值]
     */
    reg: function(id, out_id, rule) {
        var x = document.getElementById(id);
        var y = document.getElementById(out_id);
        var value = x.value;
        var list = rule.split('#');
        var e = list[0]; //非空
        var l = list[1]; //Length
        var r = list[2]; //Rule
        y.innerHTML = r + ' OK!';
        y.setAttribute('style', 'color:green;');
        if (e) {
            if (!value) {
                y.innerHTML = r + ' can\'t be empty!';
                y.setAttribute('style', 'color:red;');
                return false;
            }
        }
        if (l) {
            var tlist = l.split('.');
            var min_length = tlist[0];
            var max_length = tlist[1];
            var len = value.length;
            if (len > max_length || len < min_length) {
                y.innerHTML = r + '\'s length should between ' + min_length + ' to ' + max_length;
                y.setAttribute('style', 'color:red;');
                return false;
            }
        }
        if (r) {
            var patten = this[r];
            if (!patten.test(value)) {
                y.innerHTML = 'Invalid ' + r + ' format!';
                y.setAttribute('style', 'color:red;');
                return false
            }
        }
        return true;

    }
}